﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;

namespace MP3
{
    public class List_Operations
    {
        List<string> listOfFiles;

        public List_Operations()
        {
            listOfFiles = new List<string>();
        }
        public void load_files(ref ListBox lB)
        {
            Microsoft.Win32.OpenFileDialog dialog = new Microsoft.Win32.OpenFileDialog();
            dialog.DefaultExt = ".mp3";
            dialog.Filter = "MP3 Files (*.mp3)|*.mp3";
            dialog.Multiselect = true;
            Nullable<bool> result = dialog.ShowDialog();

            if (result == true)
            {
                foreach (string name in dialog.FileNames)
                {
                    load_file(ref lB, name);
                }
            }
            else MessageBox.Show("Ошибка чтения");
        }

        private void load_file(ref ListBox lB, string name)
        {
            try
            {
                var file = TagLib.File.Create(name);
                string time = file.Properties.Duration.ToString("mm\\:ss");
                string summary = Convert.ToString(listOfFiles.Count + 1) + ". " + name + time;
                listOfFiles.Add(name);
                lB.Items.Add(summary);
            }
            catch
            {
                MessageBox.Show("Ошибка чтения файла " + name);
            }
        }

        public void delete_files(ref ListBox lB)
        {
            for (int i = lB.SelectedItems.Count - 1; i >= 0; i--)
            {
                listOfFiles.RemoveAt(lB.Items.IndexOf(lB.SelectedItems[i]));
                lB.Items.RemoveAt(lB.Items.IndexOf(lB.SelectedItems[i]));
            }
            recount(ref lB);

        }

        private void recount(ref ListBox lB)
        {
            for (int i = 0; i < lB.Items.Count; i++)
            {
                int index_dot = 0;
                while (lB.Items[i].ToString()[index_dot] != '.')
                {
                    index_dot++;
                }
                string line = lB.Items[i].ToString().Remove(0, index_dot);
                line = line.Insert(0, (i + 1).ToString());
                if (lB.SelectedItems.Contains(lB.Items[i]))
                {
                    int selected_index = lB.SelectedItems.IndexOf(lB.Items[i]);
                    lB.Items[i] = line;
                    lB.SelectedItems.Insert(selected_index, lB.Items[i]);
                }
                else lB.Items[i] = line;

            }
        }

        public void upper_in_list(ref ListBox lB)
        {
            if (lB.Items.IndexOf(lB.SelectedItems[0]) != 0)
            {
                for (int i = 0; i < lB.Items.Count; i++)
                {
                    if (lB.SelectedItems.Contains(lB.Items[i]))
                    {
                        int selected_index = lB.SelectedItems.IndexOf(lB.Items[i]);
                        int old_index = i;
                        string a = listOfFiles[old_index - 1];
                        listOfFiles[old_index - 1] = listOfFiles[old_index];
                        listOfFiles[old_index] = a;

                        a = lB.Items[old_index - 1].ToString();
                        lB.Items[old_index - 1] = lB.Items[old_index].ToString();
                        lB.Items[old_index] = a;
                        lB.SelectedItems.Insert(selected_index, lB.Items[old_index - 1]);
                    }
                }
                recount(ref lB);
            }
        }

        public void lower_in_list(ref ListBox lB)
        {
            if (lB.Items.IndexOf(lB.SelectedItems[lB.SelectedItems.Count - 1]) != lB.Items.Count - 1)
            {
                for (int i = lB.Items.Count - 1; i >= 0; i--)
                {
                    if (lB.SelectedItems.Contains(lB.Items[i]))
                    {
                        int selected_index = lB.SelectedItems.IndexOf(lB.Items[i]);
                        int old_index = i;

                        string a = listOfFiles[old_index + 1];
                        listOfFiles[old_index + 1] = listOfFiles[old_index];
                        listOfFiles[old_index] = a;

                        a = lB.Items[old_index + 1].ToString();
                        lB.Items[old_index + 1] = lB.Items[old_index].ToString();
                        lB.Items[old_index] = a;
                        lB.SelectedItems.Insert(selected_index, lB.Items[old_index + 1]);
                    }
                }
                recount(ref lB);
            }
        }

        public void save_list(ListBox lB)
        {
            Microsoft.Win32.SaveFileDialog dialog = new Microsoft.Win32.SaveFileDialog();
            dialog.AddExtension = true;
            dialog.DefaultExt = ".pl";
            dialog.Filter = "Playlist Files (*.pl)|*.pl";

            Nullable<bool> result = dialog.ShowDialog();

            if (result == true)
            {
                string path_file = dialog.FileName;
                string text = "";
                for (int i = 0; i < listOfFiles.Count - 1; i++)
                    text += listOfFiles[i] + "\n";
                text += listOfFiles[listOfFiles.Count - 1];

                try
                {
                    System.IO.File.WriteAllText(path_file, text);

                }
                catch
                {
                    MessageBox.Show("Ошибка при сохранении");
                }
            }
        }

        public void load_list(ref ListBox lB)
        {
            Microsoft.Win32.OpenFileDialog dialog = new Microsoft.Win32.OpenFileDialog();
            dialog.AddExtension = true;
            dialog.DefaultExt = ".pl";
            dialog.Filter = "PlayList Files (*.pl)|*.pl";

            Nullable<bool> result = dialog.ShowDialog();

            if (result == true)
            {
                try
                {
                    string get = System.IO.File.ReadAllText(dialog.FileName);
                    List<string> list = new List<string>();
                    string[] someLines = get.Split('\n');
                    lB.Items.Clear();
                    for (int i = 0; i < someLines.Length; i++)
                        load_file(ref lB, someLines[i]);
                }
                catch
                {
                    MessageBox.Show("Ошибка при открытии");
                }
            }
        }




    }
}
